package cn.bud.msgbus.server.client.provider;

import com.zl9c.general.bus.bean.BusURL;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * ProviderProxy
 *
 * @author hxj
 * @version 1.0
 * @description
 * @date 2024-07-15 9:10
 */
public class ProviderProxy {

    Class<?> providerClazz;

    String busName;
    String busBeanName;
    BusURL busUrl;
    Object proxyObj;

    public ProviderProxy(Class<?> providerClazz, InvocationHandler handler) {
        SubscribeProvideClient spcAnnotation = providerClazz.getAnnotation(SubscribeProvideClient.class);
        busBeanName = providerClazz.getSimpleName().substring(0,1).toLowerCase()+providerClazz.getSimpleName().substring(1);
        if (StringUtils.isBlank(spcAnnotation.name())) {
            busName = busBeanName;
        }else {
            busName = spcAnnotation.name();
        }
        busUrl = new BusURL(spcAnnotation.url());

        proxyObj = Proxy.newProxyInstance(
                providerClazz.getClassLoader(),
                new Class[] { providerClazz },
                handler);
        this.providerClazz = providerClazz;
    }


    public Class<?> getProviderClazz() {
        return providerClazz;
    }

    public String getBusName() {
        return busName;
    }

    public String getBusBeanName() {
        return busBeanName;
    }

    public BusURL getBusUrl() {
        return busUrl;
    }

    public Object getProxyObj() {
        return proxyObj;
    }
}
